package cn.tedu.csmall.gateway.web;

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 向前端响应结果的类
 */

@Data
@Accessors(chain = true)
public class JsonResult {
    /**
     * 操作结果的状态码
     */
    private Integer state;
    private String message;
    private Object data;

    /**
     * 生成表示“成功”的响应对象
     */
    public static JsonResult ok() {
        return ok(null);
    }

    /**
     * 响应成功的结果
     */
    public static JsonResult ok(Object data) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(ServiceCode.OK.getValue());
        jsonResult.setData(data);
        return jsonResult;
    }

    /**
     * 响应失败的结果
     */
    public static JsonResult fail(ServiceCode serviceCode, String message) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setState(serviceCode.getValue());
        jsonResult.setMessage(message);
        return jsonResult;
    }
}
